commit: Support writing orphans
authorColin Walters <walters@verbum.org>
Tue, 29 Mar 2016 14:53:50 +0000 (10:53 -0400)
committerColin Walters (automation) <walters+githubbot@verbum.org>
Wed, 30 Mar 2016 03:10:50 +0000 (03:10 +0000)
The API supports this, and it's not hard for us to do in the command
line as well.  One possible use case is separating "content
generation" in a separate server.

Related: https://github.com/ostreedev/ostree/pull/223

Closes: #227
Approved by: jlebon

src/ostree/ot-builtin-commit.c
tests/basic-test.sh

index 517ea70e1940274f5f2b7a5597393668e299822a..2c74d4d7923c7130d0ae876ac04336b77c79b4e2 100644 (file)
@@ -33,6 +33,7 @@
 static char *opt_subject;
 static char *opt_body;
 static char *opt_parent;
+static gboolean opt_orphan;
 static char *opt_branch;
 static char *opt_statoverride_file;
 static char **opt_metadata_strings;
@@ -72,6 +73,7 @@ static GOptionEntry options[] = {
   { "subject", 's', 0, G_OPTION_ARG_STRING, &opt_subject, "One line subject", "SUBJECT" },
   { "body", 'm', 0, G_OPTION_ARG_STRING, &opt_body, "Full description", "BODY" },
   { "branch", 'b', 0, G_OPTION_ARG_STRING, &opt_branch, "Branch", "BRANCH" },
+  { "orphan", 0, 0, G_OPTION_ARG_NONE, &opt_orphan, "Create a commit without writing a ref", NULL },
   { "tree", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_trees, "Overlay the given argument as a tree", "dir=PATH or tar=TARFILE or ref=COMMIT" },
   { "add-metadata-string", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_metadata_strings, "Add a key/value pair to metadata", "KEY=VALUE" },
   { "add-detached-metadata-string", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_detached_metadata_strings, "Add a key/value pair to detached metadata", "KEY=VALUE" },
@@ -340,10 +342,10 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
         goto out;
     }
       
-  if (!opt_branch)
+  if (!(opt_branch || opt_orphan))
     {
       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                           "A branch must be specified with --branch");
+                           "A branch must be specified with --branch, or use --orphan");
       goto out;
     }
 
@@ -374,13 +376,13 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
           parent = g_strdup (opt_parent);
         }
     }
-  else
+  else if (!opt_orphan)
     {
       if (!ostree_repo_resolve_rev (repo, opt_branch, TRUE, &parent, error))
         goto out;
     }
 
-  if (!opt_subject && !opt_body)
+  if (!opt_subject && !opt_body && !opt_orphan)
     {
       if (!commit_editor (repo, opt_branch, &opt_subject, &opt_body, cancellable, error))
         goto out;
@@ -563,7 +565,10 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
             }
         }
 
-      ostree_repo_transaction_set_ref (repo, NULL, opt_branch, commit_checksum);
+      if (opt_branch)
+        ostree_repo_transaction_set_ref (repo, NULL, opt_branch, commit_checksum);
+      else
+        g_assert (opt_orphan);
 
       if (!ostree_repo_commit_transaction (repo, &stats, cancellable, error))
         goto out;
index 4b46fc108207f76a750134bc2e38b6a0a5019a4c..1ae7e7a4419060e4ac51b051bb56da45250c84d9 100755 (executable)
@@ -19,7 +19,7 @@
 
 set -euo pipefail
 
-echo "1..52"
+echo "1..53"
 
 $OSTREE checkout test2 checkout-test2
 echo "ok checkout"
@@ -113,6 +113,16 @@ $OSTREE commit -b test2-custom-parent -s '' --parent=${prevparent} $test_tmpdir/
 assert_streq $($OSTREE log test2-custom-parent |grep '^commit' | wc -l) "3"
 echo "ok commit custom parent"
 
+cd ${test_tmpdir}
+orphaned_rev=$($OSTREE commit --orphan -s '' $test_tmpdir/checkout-test2-4)
+$OSTREE ls ${orphaned_rev} >/dev/null
+$OSTREE prune --refs-only
+if $OSTREE ls ${orphaned_rev} 2>err.txt; then
+    assert_not_reached "Found orphaned commit"
+fi
+assert_file_has_content err.txt "No such metadata object"
+echo "ok commit orphaned"
+
 cd ${test_tmpdir}
 $OSTREE diff test2^ test2 > diff-test2
 assert_file_has_content diff-test2 'D */a/5'